layui.use(['table', 'ax', 'form', 'laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var form = layui.form;
    var laydate = layui.laydate;
    /**
     * 自定义校验规则
     */
    form.verify({
        double: function (value) {
            if (/^\d+$/.test(value) == false && /^\d+\.\d+$/.test(value) == false) {
                return '你输入的不是数字';
            }
        }
    });
    /**
     * 管理
     */
    var BtyExpend = {
        tableId: "btyExpendTable"
    };

    /**
     * 初始化表格的列
     */
    BtyExpend.initColumn = function () {
        return [[
            // {type: 'checkbox'},
            {type: 'numbers', align: 'center', title: '序号'},
            {field: 'shopCode', hide: true, sort: true, title: ''},
            {field: 'shopName', sort: true, title: '店名', width: 200},
            {
                field: 'time', sort: true, title: '花费时间', templet: function (data) {
                    return layui.util.toDateString(data.time, "yyyy-MM-dd");
                }
            },
            {field: 'remark', sort: true, title: '事项'},
            {field: 'money', sort: true, title: '金额(元)', totalRow: true},
            {field: 'createUser', hide: true, sort: true, title: '添加人'},
            {field: 'createTime', sort: true, title: '添加时间'},
            // {field: 'modifyUser', sort: true, title: ''},
            // {field: 'modifyTime', sort: true, title: ''},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BtyExpend.search = function () {
        var queryData = {};
        queryData['shopCode'] =$("#shopCodeSearch").val();
        table.reload(BtyExpend.tableId, {where: queryData});
    };

    /**
     * 弹出添加对话框
     */
    BtyExpend.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/btyExpend/add';
    };

    /**
     * 导出excel按钮
     */
    BtyExpend.exportExcel = function () {
        var checkRows = table.checkStatus(BtyExpend.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    BtyExpend.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/btyExpend/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyExpend.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyExpend/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BtyExpend.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyExpend.tableId,
        url: Feng.ctxPath + '/btyExpend/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        totalRow: true,
        cols: BtyExpend.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyExpend.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        BtyExpend.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyExpend.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyExpend.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyExpend.openEditDlg(data);
        } else if (layEvent === 'delete') {
            BtyExpend.onDeleteItem(data);
        }
    });

    //----------------添加-------------start------------

    //添加-门店选择列表
    var qyAjax = new $ax("/dbiShop/shopList");
    qyAjax.set("type", 2);
    var qyResult = qyAjax.start();
    $.each(qyResult.data, function (index, data) {
        //搜索的门店选择框
        $("#shopCodeSearch").append("<option value='" + data.code + "'>" + data.name + "</option>");

        //添加的门店选择框
        $("#shopCode").append("<option value='" + data.code + "'>" + data.name + "</option>");
    });
    form.render('select');

    laydate.render({
        elem: '#time',
        max: Feng.currentDate()
    });

    //添加-表单提交事件
    form.on('submit(btnSubmit)', function (data) {

        var ajax = new $ax(Feng.ctxPath + "/btyExpend/addItem", function (data) {
            Feng.success("添加成功！");
            $("#remark").val("");
            $("#money").val("");

            var queryData1 = {};
            var shopCode=$("#shopCode").val();
            queryData1['shopCode'] = shopCode;
            table.reload(BtyExpend.tableId, {where: queryData1});
            table.reload(BtyExpend.tableId);
        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        //门店名
        ajax.set("shopName", $("#shopCode").find("option:selected").text());
        ajax.set(data.field);
        ajax.start();

        return false;
    });
    //----------------添加-------------end------------
});
